package kali.wpj.kali.common;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import com.marshalchen.common.ui.ToastUtil;
import com.marshalchen.common.uimodule.materialtabs.MaterialTab;
import com.marshalchen.common.uimodule.materialtabs.MaterialTabHost;
import com.marshalchen.common.uimodule.materialtabs.MaterialTabListener;

import kali.wpj.kali.R;


public class AllRecodeActivity extends ActionBarActivity implements MaterialTabListener {

    private static final String TAG = "AllRecodeActivity";
    private ViewPager pager;
    private ViewPagerAdapter pagerAdapter;
    MaterialTabHost tabHost;
    private Resources res;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_al);

        res = this.getResources();

        // init toolbar (old action bar)
        Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.WHITE);

        tabHost = (MaterialTabHost) this.findViewById(R.id.materialTabHost);
        pager = (ViewPager) this.findViewById(R.id.viewpager);

        // init view pager
        pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(pagerAdapter);
        pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // when user do a swipe the selected tab change
                Log.i(TAG, position + "position");
                tabHost.setSelectedNavigationItem(position);
            }
        });

        // insert all tabs from pagerAdapter data
        for (int i = 0; i < pagerAdapter.getCount(); i++) {
            tabHost.addTab(
                    tabHost.newTab()
                            .setIcon(getIcon(i))
                            .setTabListener(this)
            );
        }

        //tabHost.setAccentColor(Color.BLUE);
    }

    @Override
    public void onTabSelected(MaterialTab tab) {
        // when the tab is clicked the pager swipe content to the tab position
        Log.i(TAG, tab + "----");
        pager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabReselected(MaterialTab tab) {
    }

    @Override
    public void onTabUnselected(MaterialTab tab) {
    }

    private class ViewPagerAdapter extends FragmentStatePagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public Fragment getItem(int num) {
            Fragment my = null;
            switch (num) {
                case 0:
                    my = new SystemAlFragment(AllRecodeActivity.this);
                    break;
                case 1:
                    my = new NmapFragment(AllRecodeActivity.this);
                    break;
                case 2:
                    my = new JoomalFragment(AllRecodeActivity.this);
                    break;


            }
            return my;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "系统扫描";
                case 1:
                    return "tab 2";
                case 2:
                    return "tab 3";
                default:
                    return null;
            }
        }

    }

    /*
     * It doesn't matter the color of the icons, but they must have solid colors
     */
    private Drawable getIcon(int position) {
        switch (position) {
            case 0:
                return res.getDrawable(R.drawable.material_tab_ic_group_black_24dp);
            case 1:
                return res.getDrawable(R.drawable.material_tab_ic_group_black_24dp);
            case 2:
                return res.getDrawable(R.drawable.material_tab_ic_group_black_24dp);

        }
        return null;
    }


}